from django.db.models.query import QuerySet
from django.views.generic import FormView, CreateView, UpdateView, DeleteView,ListView,DetailView
from django.urls import reverse_lazy
from .models import PersonInfo
from .forms import PersonInfoForm
from django.views.generic.dates import MonthArchiveView,WeekArchiveView
from django.urls import path,include

class PersonInfoMonthArchiveView(MonthArchiveView):
    allow_empty=True
    allow_future=True
    context_object_name='mylist'
    success_url='/result'
    model=PersonInfo
    date_field='hire_date'
    queryset=PersonInfo.objects.all()
    year_format='%Y'
    month_format='%m'
    paginate_by=50
    model = PersonInfo
    template_name = 'myapp/person_info_month_archive.html'
    date_field = 'hire_date'

class PersonInfoWeekArchiveView(WeekArchiveView):
    allow_empty = True
    allow_future = True
    context_object_name = 'mylist'
    template_name = 'myapp/person_info_week_archive.html'
    model = PersonInfo
    date_field = 'hire_date'
    queryset = PersonInfo.objects.all()
    year_format = '%Y'
    week_format = '%W'
    paginate_by = 50

class PersonInfoFormView(FormView):
    template_name = 'myapp/person_info_form.html'
    form_class = PersonInfoForm
    success_url = reverse_lazy('person_info_list')

    def form_valid(self, form):
        form.save()
        return super().form_valid(form)

class PersonInfoCreateView(CreateView):
    model = PersonInfo
    template_name = 'myapp/person_info_form.html'
    form_class = PersonInfoForm
    success_url = reverse_lazy('person_info_list')

class PersonInfoUpdateView(UpdateView):
    model = PersonInfo
    template_name = 'myapp/person_info_form.html'
    form_class = PersonInfoForm
    success_url = reverse_lazy('person_info_list')

class PersonInfoDeleteView(DeleteView):
    model = PersonInfo
    template_name = 'myapp/person_info_confirm_delete.html'
    success_url = reverse_lazy('person_info_list')

class PersonInfoListView(ListView):
    model=PersonInfo
    template_name='myapp/person_info_list.html'
    context_object_name='person_list'
    def get_queryset(self):
        queryset=super().get_queryset()
        search_query=self.request.GET.get('search')
        if search_query:
            queryset=queryset.filter(name_icontains=search_query)
        return queryset

class PersonInfoDetailView(DetailView):
    model=PersonInfo
    template_name='myapp/person_info_detail.html'
# Create your views here.

